/*<![CDATA[*/
	var BASE_CONTEXT_PATH = $('meta[name=context-path]').attr("content");
	BASE_CONTEXT_PATH = BASE_CONTEXT_PATH.substr(0, BASE_CONTEXT_PATH.length - 1);
	var setting = {
		treeId: "async",
		async: {
            enable: true,
            contentType: "application/x-www-form-urlencoded",
            type: "get",
            dataType: "json",
			url: BASE_CONTEXT_PATH+"/asyncTreeNode/findChildNodes",
			autoParam:["path=path"],	//第一个path指node.path,第一个path指入参名
            otherParam: [],
            dataFilter: null				
		},
		data: {
            simpleData: {
                enable: false,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            },				
		},
		edit: {
			enable: true,
			editNameSelectAll: true,
			showRemoveBtn: showRemoveBtn,
			showRenameBtn: showRenameBtn,
			removeTitle:'删除',
			renameTitle:'修改'
		},
		callback: {
			beforeRemove: beforeRemove,
			onRemove: onRemove,
			beforeDrag: beforeDrag,
			beforeEditName: beforeEditName
		},
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		}
	};

	var zNodes = [{name:"/", isParent:true, path:"/"}];
	$(document).ready(function(){
		$("#confSubmitBtn").bind("click", function(){
			$.ajax({
				url: BASE_CONTEXT_PATH+"/conf",
				type: 'post',
				async: true,
				timeout: 25000,
				data: {'connectString':$("#connectString").val(), 'sessionTimeout':$("#sessionTimeout").val()},
				dataType: 'json',
				success: function(ret){
					if(ret && ret.code==1){
						alert("修改配置成功");
					} else {
						alert(ret.msg || "修改配置失败");
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(textStatus || "修改配置失败");
				}
			});	
		});
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	});

	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	/**
	* 开始修改节点
	*/
	function beforeEditName(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		zTree.selectNode(treeNode);
		$('#editModal').modal();
		$('#edit_path').val(treeNode.path);
		$('#edit_data').val(treeNode.data || '');
		return false;
	}
	/**
	* 修改节点提交事件处理
	*/
	function editNode(){
		var path = $('#edit_path').val();
		var data = $('#edit_data').val();
		$.ajax({
			url: BASE_CONTEXT_PATH+"/asyncTreeNode/updateNode",
			type: 'post',
			async: true,
			data: {'_method':'put', 'path':path, 'data':data},
			dataType: 'json',
			success: function(ret){
				if(ret && ret.code==1){
					//关闭模态框
					$('#editModal').modal('hide');
					//刷新节点值
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					var treeNode = zTree.getNodesByParam('path',path)[0];
					treeNode.data = data;
					zTree.updateNode(treeNode);
					alert("修改成功");
				} else {
					alert(ret.msg || "修改失败");
				}
			}
		});			
	}
	/**
	* 开始删除节点
	*/
	function beforeRemove(treeId, treeNode) {
		var removeFlag = false;
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		zTree.selectNode(treeNode);
		if(confirm("确认删除节点---" + treeNode.name + " 吗？")){
			$.ajax({
				url: BASE_CONTEXT_PATH+"/asyncTreeNode/delNode",
				type: 'post',
				async: false,
				data: {'_method':'delete','path':treeNode.path},
				dataType: 'json',
				success: function(ret){
					if(ret && ret.code==1){
						removeFlag = true;
					}
				}
			});
		}
		return removeFlag;
	}
	/**
	* 删除节点成功
	*/
	function onRemove(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		zTree.cancelSelectedNode(treeNode);			
		alert("删除成功");
	}
	
	function showRemoveBtn(treeId, treeNode) {	//删除按钮显示与否
		return treeNode.isParent==false;
	}
	function showRenameBtn(treeId, treeNode) {	//修改按钮显示与否
		return treeNode.path != "/";
	}
	var newCount = 1;
	function addHoverDom(treeId, treeNode) {
		if (treeNode.isEphemeral == "true") {
			return false;
		}
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = '<span class="button add" id="addBtn_' + treeNode.tId
			+ '" title="新增" onfocus="this.blur();"></span>';
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			$('#addModal').modal();
			$('#add_parent_path').val(treeNode.path);
			$('#add_path').val('');
			$('#add_type').val('');
			$('#add_data').val('');
			return false;
		});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
	};
	/**
	* 新增节点提交事件处理
	*/
	function addNode(){
		var parent_path = $('#add_parent_path').val();
		var path = $('#add_path').val();
		if(parent_path == "/"){
			path = parent_path + path;
		}else {
			path = parent_path + "/" + path;
		}
		var type = $('#add_type').val();
		var data = $('#add_data').val();
		$.ajax({
			url: BASE_CONTEXT_PATH+"/asyncTreeNode/addNode",
			type: 'post',
			async: true,
			data: {'path':path, 'data':data, 'type':type},
			dataType: 'json',
			success: function(ret){
				if(ret && ret.code==1){
					//关闭模态框
					$('#addModal').modal('hide');
					//刷新节点值
					var treeNode = ret.data;
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					var parent_treeNode = zTree.getNodesByParam('path',parent_path)[0];
					parent_treeNode.isParent = true;
					zTree.updateNode(parent_treeNode);
					zTree.addNodes(parent_treeNode, {name:treeNode.name, path:treeNode.path, data:treeNode.data, isParent:treeNode.isParent, isEphemeral:(type==1||type==3)?"true":"false"});
					alert("新增成功");
				} else {
					alert(ret.msg || "新增失败");
				}
			}
		});			
	}		
/*]]>*/	